#!/bin/bash

# k8s集群安装步骤
# 一定要root用户

# systemctl 解决 Failed to *** : Access denied
setenforce 0
# kill -TERM 1

# 修改源
# cd /etc/yum.repos.d/
# rm -f CentOS-Base.repo CentOS-AppStream.repo CentOS-PowerTools.repo CentOS-centosplus.repo CentOS-Extras.repo
# curl -o CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
# yum makecache
# cd /root

# 查看当前系统
cat /etc/centos-release

# centos 8卸载podman
yum -y erase podman buildah

# 卸载当前安装的docker
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

# 系统升级需要删除 其实是一个问题
# kill -TERM 1

# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

# 安装docker仓库
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 更新系统
yum upgrade -y

# 查询docker版本
yum list docker-ce --showduplicates | sort -r

# 安装docker
yum install -y docker-ce-20.10.7-3.el8 docker-ce-cli-20.10.7-3.el8 containerd.io

# 系统升级需要删除 其实是一个问题
# kill -TERM 1

# 开启docker
systemctl start docker

# 修改国内镜像
cat <<EOF > /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn"
  ],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  }
}
EOF

# 重启docker
# kill -TERM 1
systemctl restart docker

# 配置kubelet 仓库 案例云
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

# 安装kubectl
yum install -y kubectl

# 禁用worker节点上的交换功能
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab

# 安装网络插件
for module in br_netfilter ip6_udp_tunnel ip_set ip_set_hash_ip ip_set_hash_net iptable_filter iptable_nat iptable_mangle iptable_raw nf_conntrack_netlink nf_conntrack nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat nf_nat_ipv4 nf_nat_masquerade_ipv4 nfnetlink udp_tunnel veth vxlan x_tables xt_addrtype xt_conntrack xt_comment xt_mark xt_multiport xt_nat xt_recent xt_set xt_statistic xt_tcpudp; do
    if ! lsmod | grep -q $module; then
        echo "module $module is not present, try to install...";
        modprobe $module
        if [ $? -eq 0 ]; then
            echo -e "\033[32;1mSuccessfully installed $module!\033[0m"
        else
            echo -e "\033[31;1mInstall $module failed!!!\033[0m"
        fi;
    fi;
done

# 查看网络插件
for module in br_netfilter ip6_udp_tunnel ip_set ip_set_hash_ip ip_set_hash_net iptable_filter iptable_nat iptable_mangle iptable_raw nf_conntrack_netlink nf_conntrack nf_conntrack_ipv4   nf_defrag_ipv4 nf_nat nf_nat_ipv4 nf_nat_masquerade_ipv4 nfnetlink udp_tunnel veth vxlan x_tables xt_addrtype xt_conntrack xt_comment xt_mark xt_multiport xt_nat xt_recent xt_set  xt_statistic xt_tcpudp; do
  if ! lsmod | grep -q $module; then
	echo "module $module is not present";
  fi;
done

# 配置网络
echo net.bridge.bridge-nf-call-iptables=1 >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf


# 系统增加用户组
# 创建用户
useradd rkeuser
# 给用户设置密码
# echo "rkeuser" | passwd rkeuser
echo "rkeuser" | passwd rkeuser --stdin > /dev/null 2>&1
# 将用户添加到docker组
usermod -aG docker rkeuser

# 开放端口6443 2379
iptables -A INPUT -p tcp --dport 6443 -j ACCEPT
iptables -A INPUT -p tcp --dport 2379 -j ACCEPT
iptables -A INPUT -p tcp --dport 30880 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 系统升级需要删除 其实是一个问题
# kill -TERM 1

# ssh配置
echo "AllowTcpForwarding yes" >> /etc/ssh/sshd_config
systemctl restart sshd

# centos/redhat
yum install -y socat

# 监听事件流的工具
yum install -y conntrack

yum install -y nfs-utils

# kk安装区域
echo 'export KKZONE=cn' >> /root/.bash_profile
source /root/.bash_profile
env
